package com.kehutong.house.admin;

import com.kehutong.common.DoveClient;
import com.kehutong.common.util.Token;
import com.kehutong.house.entity.SupplierConfig;
import org.coraframework.inject.Inject;
import org.coraframework.json.JSONObject;
import org.coraframework.logger.Logger;
import org.coraframework.logger.LoggerFactory;
import org.coraframework.mvc.MvcAction;
import org.coraframework.mvc.http.ReqMapping;
import org.coraframework.mvc.http.result.Result;
import org.coraframework.orm.jdbc.JdbcSession;
import org.coraframework.util.UUIDGenerator;

import java.util.List;

/**
 * TODO
 * 供应商调用配置、回调地址设置
 * @author zhouweipeng
 * @version 1.0
 * @date 2021/8/23 17:27
 */
@MvcAction
@ReqMapping("/admin/supplier/config")
public class YslConfigController {
    private Logger log= LoggerFactory.getLogger(YslConfigController.class);
    @Inject
    private JdbcSession jdbcSession;
    @Inject
    private DoveClient doveClient;
    @ReqMapping("/save")
    public Object saveConfig(JSONObject jsonObject, Token token){
        try {
            log.info("配置供应商参数json==={}",jsonObject.toJSONString());
            String ext=jsonObject.getString("ext"); //配置信息
            String supplierId=jsonObject.getString("supplierId"); //供应商id
            String serviceUrl=jsonObject.getString("serviceUrl"); //调用服务类
            String code=jsonObject.getString("code");  //供应商编码
            List<SupplierConfig> supplierConfigList=jdbcSession.findArray(SupplierConfig.class).eq("supplierId",supplierId).exe();
            SupplierConfig supplierConfig=null;
            if(supplierConfigList.size()==0){
                supplierConfig=new SupplierConfig(UUIDGenerator.randomUUID());
                supplierConfig.setCode(code);
                supplierConfig.setExt(ext);
                supplierConfig.setServiceUrl(serviceUrl);
                supplierConfig.setSupplierId(supplierId);
                jdbcSession.insert(supplierConfig);
            }else{
                supplierConfig=supplierConfigList.get(0);
                supplierConfig.setCode(code);
                supplierConfig.setExt(ext);
                supplierConfig.setServiceUrl(serviceUrl);
                jdbcSession.updateById(supplierConfig);
            }
        } catch (Exception e) {
            log.error("配置供应商异常{}",e);
        }
        return Result.success();
    }
    @ReqMapping("/setHeartBeatUrl")
    public Object setHeartBeatUrl(JSONObject jsonObject,Token token){
        try {
            String supplierId=jsonObject.getString("supplierId"); //供应商id
            String notifyUrl=jsonObject.getString("notifyUrl"); //回调地址
            SupplierConfig supplierConfig=jdbcSession.findOne(SupplierConfig.class).eq("supplierId",supplierId).exe();
            if(supplierConfig==null){
                return Result.error(0, "请配置设备供应商");
            }
            String method="setHeartBeat";
            String url=supplierConfig.getServiceUrl()+method; //接口请求地址
            log.info("心跳回调地址设置接口url=========={}",url);
            JSONObject result = doveClient.post(url, http->{
                JSONObject body = new JSONObject();
                body.put("notifyUrl", notifyUrl);
                body.put("ext",supplierConfig.getExt());
                http.setBody(body.toString());
            });
            log.info("心跳回调地址设置接口返回{}",result);
        } catch (Exception e) {
            log.error("伊时利配置设备心跳地址异常{}",e);
        }
        return Result.success();
    }
    @ReqMapping("/setRecordCallBackUrl")
    public Object setRecordCallBackUrl(JSONObject jsonObject,Token token){
        try {
            String supplierId=jsonObject.getString("supplierId"); //供应商id
            String notifyUrl=jsonObject.getString("notifyUrl"); //回调地址
            SupplierConfig supplierConfig=jdbcSession.findOne(SupplierConfig.class).eq("supplierId",supplierId).exe();
            if(supplierConfig==null){
                return Result.error(0, "请配置设备供应商");
            }
            String method="setRecordCallBack";
            String url=supplierConfig.getServiceUrl()+method; //接口请求地址
            log.info("通行记录回调地址设置接口url=========={}",url);
            JSONObject result = doveClient.post(url, http->{
                JSONObject body = new JSONObject();
                body.put("notifyUrl", notifyUrl);
                body.put("ext",supplierConfig.getExt());
                http.setBody(body.toString());
            });
            log.info("通行记录回调地址设置接口返回{}",result);
        } catch (Exception e) {
            log.error("伊时利配置设备通行记录地址异常{}",e);
        }
        return Result.success();
    }

}
